import request from '@/utils/request'
import { fd } from '@/utils'
import Axios from 'axios'
import store from '@/store'
import { microservices } from '@/utils'

// 是否需要验证码
export function VerificationCode () {
  return request({
    // url: `${microservices.pmsSys}/validateCode/queryEnableLoginValidateCode`,
    url: `${microservices.pmsSys}/nl/validateCode/queryEnableLoginValidateCode`,
    method: 'get'
  })
}
// 获取验证码
export function VerificationCodeImg () {
  return request({
    // url: `${microservices.pmsSys}/validateCode/image`,
    url: `${microservices.pmsSys}/nl/validateCode/image`,
    method: 'get',
    responseType: 'blob'
  })
}

export function login (res) {
  let transfer = JSON.stringify(res)
  let data = JSON.parse(transfer)
  if (data.checkCode == '') {
    let obj = {}
    let arr = Object.keys(data)
    arr.forEach(item => {
      if (data[item] != '') {
        obj[item] = data[item]
      }
    })
    return request({
      url: `${microservices.pmsSys}/nl/token/loginByUsername`,
      data: fd(obj),
      method: 'post'
    })
  } else {
    // data.checkCodeId = md5(data.checkCodeId)
    return request({
      url: `${microservices.pmsSys}/nl/token/loginByUsername`,
      data: fd(data),
      method: 'post'
    })
  }
}

export function tokenOut () {
  return request({
    url: `${microservices.pmsSys}/token/tokenOut`,
    method: 'DELETE'
  })
}

export function getSysMember (params) {
  let sortStr = params.sortStr
  delete params.sortStr
  return request({
    url: `${microservices.pmsSys}/SysMember?${sortStr}`,
    params
  })
}

export function getSysMemberSort (params) {
  return request({
    url: `${microservices.pmsSys}/SysMember?currPage=${params.currPage}&pageSize=${params.pageSize}&memberUsername=${params.memberUsername}&memberName=${params.memberName}${params.sortList}`
  })
}

export function postSysMemberAble (id) {
  return request({
    url: `${microservices.pmsSys}/SysLoginUsername/able?ids=${id}`,
    method: 'post'
  })
}

export function getSysMemberCurrMemInfo () {
  return Axios({
    url: `${process.env.VUE_APP_BASE_API}${microservices.pmsSys}/SysMember/currMemInfo`,
    headers: {
      token: store.getters.token ? store.getters.token : ''
    }
  })
}

export function getSysMenuCurrTree (systemCode) {
  return request({
    url: `${microservices.pmsSys}/SysMenu/currTree?systemCode=${systemCode}`,
    method: 'get'
  })
}

// 密码校验
export function postcheckPasswordRule (data) {
  return request({
    url: `${microservices.pmsSys}/SysLoginUsername/checkPasswordRule`,
    data,
    method: 'post'
  })
}

// 新增用户
export function putSysMember (data) {
  return request({
    url: `${microservices.pmsSys}/SysMember`,
    data,
    method: 'put'
  })
}

export function postSysMember (data) {
  return request({
    url: `${microservices.pmsSys}/SysMember`,
    data,
    method: 'post'
  })
}

export function getSysMessage (params) {
  return request({
    url: `${microservices.pmsSys}/SysMessage`,
    params,
    method: 'get'
  })
}

// 读取消息
export function getDoRead (id) {
  return request({
    url: `${microservices.pmsSys}/SysMessage/doRead/${id}`,
    method: 'get'
  })
}

// 修改密码
export function postUpdatePassword (data) {
  return request({
    url: `${microservices.pmsSys}/SysLoginUsername/updatePassword`,
    data,
    method: 'post'
  })
}

export function getSysRoleCurrTree () {
  return request({
    url: `${microservices.pmsSys}/SysRole/currTree`
  })
}

export function postSysMemberRole (data) {
  return request({
    url: `${microservices.pmsSys}/SysMemberRole`,
    data,
    method: 'post'
  })
}

export function getSysOrgDepartCurrTree (params) {
  return request({
    url: `${microservices.pmsSys}/SysOrgDepart/currTree`,
    params,
    method: 'get'
  })
}

export function getSysMemberId (id) {
  return request({
    url: `${microservices.pmsSys}/SysMember/${id}`
  })
}

export function postResetPassword (data) {
  return request({
    url: `${microservices.pmsSys}/SysLoginUsername/resetPassword`,
    data,
    method: 'post'
  })
}

export function getqueryByMemId (id) {
  return request({
    url: `${microservices.pmsSys}/SysMemberRole/queryByMemId/${id}`
  })
}

export function getNlQueryFsUrl () {
  return request({
    url: `${microservices.pmsSys}/nl/queryFsUrl`
  })
}

// 获取共有的fs图片服务
export function postUpload (data) {
  return request({
    url: `${microservices.pmsFs}/fileCommon`,
    data,
    method: 'post'
  })
}

export function postUploadFilePrivate (data) {
  return request({
    url: `${microservices.pmsFs}/filePrivate`,
    data,
    method: 'post'
  })
}
